<!DOCTYPE html>
<html lang="zh_CN" xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">

<!-- head 头部-->
<head th:replace="admin/layout/include::head('菜单管理','')"></head>

<body>
<!-- loading 动画-->
<div th:replace="admin/layout/include::loading"></div>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-form toolbar">
                <div class="pull-left layui-form-pane search-box">
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">搜索：</label>
                        <div class="layui-input-inline mr0">
                            <input id="edtSearch" class="layui-input" type="text" placeholder="输入关键字"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button id="btnSearch" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
                        <button id="btnAdd" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
                    </div>
                </div>
            </div>


            <!-- 数据表格 -->
            <table class="layui-table" id="data-table" lay-filter="data-table"></table>
        </div>
    </div>
</div>
</body>

<!-- 表格操作列 -->
<script type="text/html" id="data-table-bar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<!-- 表格状态列 -->
<script type="text/html" id="data-table-state">
    <input type="checkbox" lay-filter="data-table-state" value="{{d.navId}}" lay-skin="switch" lay-text="正常|禁用"
           {{d.state==0?'checked':''}}/>
</script>
<!--类型-->
<script type="text/html" id="data-table-type">
    {{#  if(d.navType == "1"){ }}
    <span style="color: #009688;">网站菜单</span>
    {{#  } else if(d.navType == "2"){ }}
    <span style="color: #5FB878;">top导航</span>
    {{#  } else if(d.navType == "3"){ }}
    <span style="color: #1E9FFF;">底部导航</span>
    {{#  } else if(d.navType == "4"){ }}
    <span style="color: #ff7714;">快速通道</span>
    {{#  } else{ }}
    unkown
    {{#  }}}
</script>
<script type="text/html" id="data-table-target">
    {{#  if(d.target == "_self"){ }}
    当前窗口打开
    {{#  } else if(d.target == "_blank"){ }}
    新窗口打开
    {{#  } else{ }}
    unkown
    {{#  }}}
</script>
<script type="text/html" id="data-table-name">
    <a href="{{d.navUrl}}" style="color:{{d.navStyle}};text-decoration: underline ;" target="_blank">{{d.navName}}</a>
</script>
<script type="text/html" id="data-table-icon">
    <span><i class="layui-icon {{d.navIcon}}"></i> </span>
</script>
<!-- js部分 -->
<div th:replace="admin/layout/include::js"></div>
<script>
    layui.use(['form', 'table', 'util', 'admin','iconPicker'], function () {
        var form = layui.form;
        var table = layui.table;
        var layer = layui.layer;
        var util = layui.util;
        var admin = layui.admin;
        var iconPicker = layui.iconPicker;
        var formUrl;  // 表单提交的action

        //渲染表格
        var tableIns = table.render({
            elem: '#data-table',
            url: '/admin/nav/',
            page : true,
			loading : true,
			height : 'full-115',
			limit : 10,
			limits : [ 10, 20, 50, 100 ],
            response:{
	        	countName: 'total'
	        },
            cols: [
                [
                {type: 'numbers'},
                {field: 'navId', title: '#'},
                {field: 'navName', title: '菜单名',templet:'#data-table-name'},
                {field: 'navUrl', sort: true, title: '菜单地址'},
                {field: 'navIcon',title: '菜单图标',templet:'#data-table-icon'},
                {field: 'navType', sort: true, title: '菜单类型',templet:'#data-table-type'},
                {field: 'target',title: '打开方式',templet:'#data-table-target'},
                {field: 'orderNo',sort:true, title: '排序'},
                {field: 'state', title: '状态',templet: '#data-table-state'},
                {
                    field: 'createTime', sort: true, templet: function (d) {
                        return util.toDateString(d.createTime);
                    }, title: '创建时间'
                },
                {align: 'center', toolbar: '#data-table-bar', title: '操作'}
            ]
            ]
        });

        // 添加按钮点击事件
        $('#btnAdd').click(function () {
            showEditModel();
        });


        // 工具条点击事件
        table.on('tool(data-table)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'del') { //删除
                doDelete(obj);
            }
        });

        // 搜索按钮点击事件
        $('#btnSearch').click(function () {
            var keyword = $('#edtSearch').val();
            table.reload('data-table', {where: {navName: keyword}});
        });

        function doDelete(obj){
            top.layer.confirm('确定要删除吗？', function (i) {
                top.layer.close(i);
                B.del({
                    url:'/admin/nav/'+obj.data.navId,
                    loading:true,
                    success:function(data){
                        if (data.success) {
                            layer.msg(data.msg, {icon: 1});
                            obj.del();
                        } else {
                            layer.msg(data.msg, {icon: 2});
                        }
                    }
                });
            });
        }
        // 显示编辑弹窗
        var showEditModel = function (data) {
            admin.putTempData("t_nav",data);
            admin.putTempData("formOk",false);
            top.layui.admin.open({
                type: 2,
                title: data ? '修改配置' : '添加配置',
                area: ['500px', '520px'],
                content: '/admin/nav/save',
                success:function(layero, dIndex) {
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                },
                end: function () {
                    admin.getTempData('formOk') && tableIns.reload({},'data');  // 成功刷新表格
                }
            });

        };
        // 修改状态
        form.on('switch(data-table-state)', function (obj) {
            B.post({
                url: '/admin/nav/updateState',
                loading: true,
                data: {navId: obj.elem.value, state: obj.elem.checked ? 0 : 1},
                success: function (data) {
                    if (data.success) {
                        layer.msg(data.msg, {icon: 1});
                    } else {
                        layer.msg(data.msg, {icon: 2});
                        $(obj.elem).prop('checked', !obj.elem.checked);
                        form.render('checkbox');
                    }
                }

            });

        });

    });

</script>
</html>